package com.example.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.db.DBHelper;
import com.example.test.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Forget extends AppCompatActivity {

    private EditText old_user, new_password, again_password;
    private Button btn_change;
    private DBHelper db;
    private SQLiteDatabase sd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        initView();
        getDataBase();
        initEvent();
    }

    private void getDataBase() {
        db = new DBHelper(this);
    }

    private void initEvent() {
        btn_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String old_id = old_user.getText().toString().trim();
                String new_pwd = new_password.getText().toString().trim();
                String again_pwd = again_password.getText().toString().trim();
                String str_password = stringFilter(new_pwd);
                String str_password1 = stringFilter(again_pwd);

                sd = db.getWritableDatabase();
                Cursor old_cursor = sd.query("student", null, "username=?",
                        new String[]{old_id}, null, null, null);
                if (old_cursor.getCount() == 1) {
                        if (new_pwd.equals(again_pwd)) {
                            if (new_pwd.equals(str_password)) {
                                ContentValues cv = new ContentValues();
                                cv.put("password", new_pwd);
                                sd.update("student", cv, "username=?", new String[]{old_id});
                                Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                Intent in = new Intent(getApplicationContext(), Login.class);
                                startActivity(in);
                                finish();
                            } else {
                                Toast.makeText(getApplicationContext(), "密码中含有特殊字符，请重新输入", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(getApplicationContext(), "两次密码输入不同，请重新输入", Toast.LENGTH_SHORT).show();
                        }
                }else {
                        Toast.makeText(getApplicationContext(), "该用户未注册，请先去注册", Toast.LENGTH_SHORT).show();
                    }
                }
        });
    }

    private void initView() {
        old_user = (EditText) findViewById(R.id.et_forget_user);
        new_password = (EditText) findViewById(R.id.et_forget_pwd1);
        again_password = (EditText) findViewById(R.id.et_forget_pwd2);
        btn_change = findViewById(R.id.btn_forget);
    }

    //判断账号或密码为数字或字母
    public static String stringFilter(String str) throws PatternSyntaxException {
        String regEx = "[^a-zA-Z0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
}
